﻿/* 
this is a jquery selector extension to add regex to selectors 
it is slightly modified from the original to allow getting directly 
at element properties
    
some examples:
    
a:regex(elem:href,/ico/) -- find all links with ico in href, it uses elem 
prefix because the jquery attr method returns relative url not qualified
span:regex(css:width,em) -- find all spans with css width's specified in em
input:regex(name,.*Amount[0-9]) -- find all inputs whose name ends with amount and a number
    
*/
//taken from: http://james.padolsey.com/javascript/regex-selector-for-jquery/ 
(function () {
    jQuery.expr[':'].regex = function (elem, index, match) {
        var matchParams = match[3].split(','),
        validLabels = /^(data|css|elem):/,
        attr = {
            method: matchParams[0].match(validLabels) ?
                        matchParams[0].split(':')[0] : 'attr',
            property: matchParams.shift().replace(validLabels, '')
        },
        regexFlags = 'ig',
        regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g, ''), regexFlags);
        return regex.test(attr.method == 'elem' ? elem[attr.property] : jQuery(elem)[attr.method](attr.property));
    };
})();

(function () {
    //fast trim function
    //http://blog.stevenlevithan.com/archives/faster-trim-javascript
    String.prototype.trim = function() {
        var	str = this.replace(/^\s\s*/, ''),
		    ws = /\s/,
		    i = str.length;
	    while (ws.test(str.charAt(--i)));
	    return str.slice(0, i + 1);
    };

    //readyQueue is defined within the anonymous function, which allows is to get minified.
    var AscendOne = window.a1 = {
        execWhenReady: function (name, callback, obj) {
            /// <summary>
            ///     This function accepts a queue name and a callback function to execute when the queue is ready.
            /// </summary>
            /// <param name="name" type="String">
            ///     The name of the queue to place the callback on.
            /// </param>
            /// <param name="callback" type="Object">
            ///     The function to execute when the queue is set to ready.
            /// </param>

            obj = obj || AscendOne;

            //If the queue does not exist, we execute the callback function immediately.
            if ($(obj).data('ready-queue') !== undefined) {
                callback();
                return;
            }

            //Add the callback to the named queue. To continue the queue we must call dequeue.
            $(obj).queue(name, function (next) { callback(); next(); });
        },

        setReady: function (name, obj) {
            /// <summary>
            ///     This function sets the named queue to ready and starts to dequeue the callbacks.
            /// </summary>
            /// <param name="name" type="String">
            ///     The name of the queue to place the callback on.
            /// </param>

            obj = obj || AscendOne;

            $(obj).data('ready-queue', true);
            $(obj).dequeue(name);
        },

        throbberMarkup: '<div class="throbber"><img alt="Loading, please wait" src="/content/throbber.gif" /></div>',

        //TODO: make this a jquery plugin or something so i can call it on selectors...
        throbber: function (throbberTarget, callback, markup) {
            markup = markup || AscendOne.throbberMarkup;
            var throbber = $(markup).appendTo(throbberTarget);
            //yield since this is long running operation
            if(callback) setTimeout(function() { callback(function() { throbber.remove(); }) }, 100);
        }
    };

})();

